package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.Pms_Attr_Mapper;
import com.shop.mapper.Pms_Category_Attr_Mapper;
import com.shop.pojo.pms.Pms_Attr;
import com.shop.pojo.pms.Pms_Category_Attr;
import com.shop.service.Pms_Attr_Service;
import com.shop.service.Pms_Category_Attr_Service;
import com.shop.to.AttrTo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//属性业务实现层
@Service
public class Pms_Attr_ServiceImpl implements Pms_Attr_Service {
    @Autowired
    private Pms_Attr_Mapper pms_attr_mapper;
    @Autowired
    private Pms_Category_Attr_Mapper pms_category_attr_mapper;

    /*
     * @Description 查询全部属性,要通过分组防止重复
     * @param
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/28 21:36
     */
    @Override
    public R findAllAttr() {
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.groupBy("attr_name");
        R r1=new R();
        r1.data(pms_attr_mapper.selectList(queryWrapper));
        return r1;
    }
    /*
     * @Description  根据ID来对属性表进行删除
     * 然后在通过这个属性id去属性分类表里去找，如果找到了的话则删除里面这个属性id的全部数据
     * @param attr_id
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/10/29 19:28
     */
    @Transactional
    @Override
    public R delPms_Attr(Integer attr_id) {

        int delete = pms_attr_mapper.deleteById(attr_id);

        if(delete>0){
            QueryWrapper<Pms_Category_Attr> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("attr_id",attr_id);
            int delete1 = pms_category_attr_mapper.delete(queryWrapper);
            if(delete1>0){
                return R.success();
            }
        }
        return R.error(500,"服务器异常");
    }

    @Override
    public Pms_Attr PMS_ATTRById(Long attr_id) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("attr_id",attr_id);
        return pms_attr_mapper.selectOne(queryWrapper);
    }
    /*
     * @Description 根据当前分类ID和分组ID来查询当前属性信息
     * @param attr_group_id
     * @param catelog_id
     * @return java.util.List<com.shop.pojo.pms.Pms_Attr>
     * @author liusupeng
     * @date 2020/11/1 15:03
     */
    @Override
    public List<Pms_Attr> findAllAttrByCatelogIdAndGroupId(Long attr_group_id, Long catelog_id) {

        return pms_attr_mapper.findAllAttrByCatelogIdAndGroupId(attr_group_id, catelog_id);
    }
    /*
     * @Description 根据分类ID来查询当前分类下的全部销售属性
     * @param catelog_id
     * @return java.util.List<com.shop.pojo.pms.Pms_Attr>
     * @author liusupeng
     * @date 2020/11/2 21:32
     */
    @Override
    public List<Pms_Attr> findAllSaleAttr(Long catelog_id) {

        return pms_attr_mapper.findAllSaleAttr(catelog_id);
    }
    /*
     * @Description 根据属性ID来查询当前属性的值
     * @param attrId
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/12 8:50
     */
    @Override
    public AttrTo findAttrNameByAttrId(Long attrId) {
                   AttrTo attrTo=new AttrTo();
 QueryWrapper<Pms_Attr> pms_attrQueryWrapper=new QueryWrapper<>();
        pms_attrQueryWrapper.eq("attr_id",attrId);
        Pms_Attr pms_attr = pms_attr_mapper.selectOne(pms_attrQueryWrapper);
          if(pms_attr!=null){
              attrTo.setAttrId(pms_attr.getAttr_id());
              attrTo.setAttrName(pms_attr.getAttr_name());
              attrTo.setCode(200);
          }else{
              attrTo.setCode(500);
          }
        return attrTo;
    }
}
